package com.myebills.ebpsfrontend.cust.maxis;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.myebills.ebpsfrontend.cust.utils.*;
import com.checkfree.isolutions.customer.persist.Account;
import com.checkfree.isolutions.customer.persist.AccountHomeImpl;
import com.checkfree.isolutions.customer.persist.Merchant;
import com.checkfree.isolutions.customer.persist.MerchantHomeImpl;
import com.checkfree.isolutions.customer.persist.User;
import com.checkfree.isolutions.admin.ObjectId;
import com.checkfree.isolutions.statement.persist.Statement;
import com.checkfree.isolutions.statement.StatementSection;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MonthlyChargesServlet_v1r00 extends EbppxTransactionServlet {

    /**
     *
     *
     */
    public void doTransaction(HttpServletRequest req, HttpServletResponse res)
    throws ServletException {
        try {
            final HttpSession session = req.getSession(false);
            if (null == session) {
                throw new ServletException("Session must be already established");
            }

            final EbppxUrlManager urlManager = getUrlManager(req);
            
            //Get attribute from session for the logon user
            final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);

            final String phoneIndex = urlManager.getSecureParameter("phoneIndex");
            if (phoneIndex.length() == 0) {
                throw new ServletException("Missing phone index");
            }

			String accno = ecsUser.getSAccount();
			System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA = "+accno);
			System.out.println("PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP = "+phoneIndex);
			if(accno != null){
				//final String tAccountNo = (String) enumAccounts.nextElement();

                Merchant cfiMerchant = MerchantHomeImpl.singleton().findByName(ecsUser.getMerchantName());
                Account account = AccountHomeImpl.singleton().findByExternalKeyAndMerchant(accno, cfiMerchant);
                Statement cfiStatement =  StatementUtil.getStatement(account,EbppxCusConstants.STMT_START_MONTH,0);
                if(cfiStatement != null){
	        		StatementSection cfiRootSection = cfiStatement.getRootSection();
	        		StatementSection cfiSection = cfiRootSection.getSection("DetailsOfChargesSection");
	        		if(cfiSection != null){
	        			StatementSection cfiPhoneNumberSection = cfiSection.getRepeatSection("PhoneNumberSection", Integer.parseInt(phoneIndex));
	        			if(cfiPhoneNumberSection != null){
	        				String pNo = cfiPhoneNumberSection.getFieldText("PhoneNumber");
	        				req.setAttribute("PhoneNumberSection", cfiPhoneNumberSection);
	        				System.out.println("got phone number !!!!!!!!!!!!!!!!!! = "+pNo);
	        			}	
	        		}
	        		if(cfiRootSection != null){
	        			String marketSegment = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("MarketSegment"));
	        			ecsUser.setMSegment(marketSegment);
	        		}
                }
                //cfiStatements.addElement(cfiStatement);
			}

            // log activity start
               // EbppxCusDBUtil.logUserActivity(EbppxConstants.CUS_VIEW, ecsUser.getSAccount(),ecsUser.getUserId(),EbppxConstants.CUSTOMER, EbppxConstants.STATUS_VIEWED, DateUtil.formatDate(new Date()),DateUtil.formatDate(cfiStatement.getDate()),EbppxConstants.FROM_TELSUMM,"","","");
            // log activity end

            forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);
        } catch (ServletException e) {
            // Just re-throw these
            throw e;
        } catch (Throwable t) {
            throw new ServletException(t.toString(), t);
        }
    }
}